/*
 * All content copyright http://www.j2eefast.com, unless
 * otherwise indicated. All rights reserved.
 * No deletion without permission
 */
package com.clwl.supervise.superviseapp.service.investigation;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clwl.supervise.superviseapp.entity.supervise.JgDsjXzcfDoc;
import com.clwl.supervise.superviseapp.mapper.supervise.JgDsjXzcfDocMapper;
import com.clwl.supervise.superviseapp.util.MoneyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 行政处罚文书Service接口
 *
 * @author: GuXueJian
 * @date 2023-10-10 15:34:58
 */
@Service
public class JgDsjXzcfDocService extends ServiceImpl<JgDsjXzcfDocMapper, JgDsjXzcfDoc> {

    @Resource
    private JgDsjXzcfDocMapper jgDsjXzcfDocMapper;

    @DS("ads")
    public Map<String, Object>  getJgDsjXzcfDocByInputStr(String inputContent) {
        Map<String, Object> resultMap=new HashMap<>();
        Map<String, Object> dataMap=new HashMap<>();
        List<JgDsjXzcfDoc> list = jgDsjXzcfDocMapper.findByInstr(inputContent,5);
        if (CollectionUtils.isEmpty(list)) {
            resultMap.put("code","1");
            resultMap.put("msg","没有匹配到执法案例。");
            return resultMap;
        }
        List<Map<String, Object>> bbList = new ArrayList<>();
        Set<String> cfyjSet = new HashSet<>();
        int yq = 0;
        int erq = 0;
        int sq = 0;
        int wq = 0;
        int yw = 0;
        int dy = 0;

        //List<String> cfyjList=new LinkedList<>();
        for (JgDsjXzcfDoc xzcf : list) {
            Map<String, Object> xzcfMap = new HashMap<>();
            StringBuffer zfalBuffer = new StringBuffer();
            xzcfMap.put("id",xzcf.getId());
            xzcfMap.put("content",xzcf.getContent());
            xzcfMap.put("cfyj",xzcf.getCfyj());
            xzcfMap.put("cfnr",xzcf.getCfnr());
            xzcfMap.put("dsr",xzcf.getDsr());
            //处理
            String cfBookName = "";
            if (StringUtils.isNotBlank(xzcf.getContent())) {
                String content = xzcf.getContent().replaceAll("<br />", "<br/>");
                String[] contentArray = content.split("<br/>");
                if (null != contentArray && contentArray.length > 0) {
                    for (String cte : contentArray) {
                        if (StringUtils.isNotBlank(cte)) {
                            if (cte.indexOf("书") > -1) {
                                cfBookName = cte;
                                break;
                            }
                        }
                    }
                }
            }
            if (StringUtils.isNotBlank(cfBookName)) {
                zfalBuffer.append(xzcf.getCfjg());
                zfalBuffer.append("、");
                zfalBuffer.append(xzcf.getDsr());
                zfalBuffer.append(cfBookName);
                xzcfMap.put("zfalName",zfalBuffer.toString());
            }else{
                xzcfMap.put("zfalName","");
            }
            //处罚内容 1；罚款柒仟元；
            String cfnr = xzcf.getCfnr();

            if (StringUtils.isNotBlank(xzcf.getCfzl())) {
                //处罚治理 eg:罚款,没收违法所得,警告....警告,罚款,没收非法财物
                String[] cfzlArray = xzcf.getCfzl().split(",");
                if (null != cfzlArray && cfzlArray.length > 0) {
                    for (String cfzl : cfzlArray) {
                        if (cfzl.indexOf("罚款") > -1) {
                            if (StringUtils.isNotBlank(cfnr)) {
                                if (cfnr.indexOf("br") > -1) {
                                    cfnr.replaceAll("<br />", "<br/>");
                                    String[] cfnrArray = cfnr.split("<br/>");
                                    if (null != cfnrArray && cfnrArray.length > 0) {
                                        for (String cnl : cfnrArray) {
                                            String[] cn1fArray = cnl.split("；");
                                            for (String cnlf : cn1fArray) {
System.out.println("===="+cnlf);
                                                if (cnlf.indexOf("罚款") > -1) {
                                                    //处理罚款项 解析罚款金额
                                                    String money = MoneyUtil.convertAmount(cnl);
                                                    if (null != money) {
                                                        Double dbamount = Double.parseDouble(money);
                                                        if (dbamount > 0D && dbamount <= 1000D) {
                                                            yq += 1;
                                                        }
                                                        if (dbamount > 1000D && dbamount <= 2000D) {
                                                            erq += 1;
                                                        }
                                                        if (dbamount > 2000D && dbamount <= 3000D) {
                                                            sq += 1;
                                                        }
                                                        if (dbamount > 3000D && dbamount <= 5000D) {
                                                            wq += 1;
                                                        }
                                                        if (dbamount > 5000D && dbamount <= 10000D) {
                                                            yw += 1;
                                                        }
                                                        if (dbamount > 10000D ) {
                                                            dy += 1;
                                                        }
                                                        String content = "罚款" + String.valueOf(dbamount) + "元";
                                                        cfyjSet.add(content);
                                                    }
                                                }
                                            }

                                        }
                                    }
                                } else {
                                    String[] cfnrArray = cfnr.split("；");
                                    if (null != cfnrArray && cfnrArray.length > 0) {
                                        for (String cnl : cfnrArray) {
                                            if (cnl.indexOf("罚款") > -1) {
                                            	System.out.println("===="+cnl);
                                                //处理罚款项 解析罚款金额
                                                String money = MoneyUtil.convertAmount(cnl);
                                                if (null != money) {
                                                    Double dbamount = Double.parseDouble(money);
                                                    if (dbamount > 0D && dbamount <= 1000D) {
                                                        yq += 1;
                                                    }
                                                    if (dbamount > 1000D && dbamount <= 2000D) {
                                                        erq += 1;
                                                    }
                                                    if (dbamount > 2000D && dbamount <= 3000D) {
                                                        sq += 1;
                                                    }
                                                    if (dbamount > 3000D && dbamount <= 5000D) {
                                                        wq += 1;
                                                    }
                                                    if (dbamount > 5000D && dbamount <= 10000D) {
                                                        yw += 1;
                                                    }
                                                    if (dbamount > 10000D ) {
                                                        dy += 1;
                                                    }
                                                    String content = "罚款" + String.valueOf(dbamount) + "元";
                                                    cfyjSet.add(content);
                                                }

                                            }

                                        }
                                    }
                                }
                            }
                        } else {
                            if(cfzl.indexOf(".")>-1){
                                cfzl = cfzl.substring(cfzl.indexOf(".", 1) + 1, cfzl.length());
                            }else if(cfzl.indexOf("、")>-1){
                                cfzl = cfzl.substring(cfzl.indexOf("、", 1) + 1, cfzl.length());
                            } else if(cfzl.indexOf("；")>-1){
                                cfzl = cfzl.substring(cfzl.indexOf("；", 1) + 1, cfzl.length());
                            }else{
                                cfzl = cfzl.substring(cfzl.indexOf(";", 1) + 1, cfzl.length());
                            }
                            cfyjSet.add(cfzl);
                        }
                    }
                }

            }
            if(bbList.size()<5){
                bbList.add(xzcfMap);
            }
        }
        dataMap.put("yq",yq);
        dataMap.put("erq",erq);
        dataMap.put("sq",sq);
        dataMap.put("wq",wq);
        dataMap.put("yw",yw);
        dataMap.put("dy",dy);
        dataMap.put("bbList",bbList);
        dataMap.put("cfyjList",cfyjSet);
        resultMap.put("code","0");
        resultMap.put("msg","成功");
        resultMap.put("data",dataMap);
return resultMap;
    }

    private Number convertAmount(String content) {
        String fraction[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "佰", "仟", "角", "分", "元", "万", "亿"};
        String nm[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        StringBuffer nmbuffer = new StringBuffer();
        StringBuffer dxbuffer = new StringBuffer();
        return null;
    }


}
